package databasetest;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.xml.bind.util.ValidationEventCollector;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Delete extends JInternalFrame {
	Connection con;
	private JButton button;
	private JLabel label;
	private JLabel label_1;
	private JLabel label_2;
	private JTextField serial;
	private JTextField ea;
	private JTextField code;
	private JButton button_1;
	private JLabel status;
	DatabaseTest d;

	public Delete(Connection c, DatabaseTest d) {
		super("delete", true, true, true, false);
		this.con = c;
		this.d = d;
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(null);
		getContentPane().add(getButton());
		getContentPane().add(getLabel());
		getContentPane().add(getLabel_1());
		getContentPane().add(getLabel_2());
		getContentPane().add(getSerial());
		getContentPane().add(getEa());
		getContentPane().add(getCode());
		getContentPane().add(getButton_1());
		getContentPane().add(getStatus());

	}

	private JButton getButton() {
		if (button == null) {
			button = new JButton("삭제");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try {
						int s = Integer.parseInt(serial.getText());
						String c = code.getText();
						int e = Integer.parseInt(ea.getText());

						String sql = "delete from ptbl where no =?";

						PreparedStatement pstmt = con.prepareStatement(sql);
						pstmt.setInt(1, s);
						pstmt.executeUpdate();
						pstmt.close();
					} catch (Exception ex) {
						ex.printStackTrace();
					}
				}
			});
			button.setBounds(85, 112, 97, 23);
		}
		return button;
	}

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("serial");
			label.setBounds(55, 87, 57, 15);
		}
		return label;
	}

	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("code");
			label_1.setBounds(55, 14, 57, 15);
		}
		return label_1;
	}

	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("ea");
			label_2.setBounds(55, 50, 57, 15);
		}
		return label_2;
	}

	private JTextField getSerial() {
		if (serial == null) {
			serial = new JTextField();
			serial.setColumns(10);
			serial.setBounds(145, 78, 116, 21);
		}
		return serial;
	}

	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setColumns(10);
			ea.setBounds(145, 47, 116, 21);
		}
		return ea;
	}

	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setColumns(10);
			code.setBounds(145, 11, 116, 21);
		}
		return code;
	}

	private JButton getButton_1() {
		if (button_1 == null) {
			button_1 = new JButton("검색");
			button_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					int s = Integer.parseInt(serial.getText());
					String sql = "select * from ptbl where serial =?";
					try {
						PreparedStatement pstmt = con.prepareStatement(sql);
						pstmt.setInt(1, s);

						ResultSet rs = pstmt.executeQuery();
						if (rs.next()) {
							code.setText(rs.getString("code"));
							ea.setText("" + rs.getInt("ea"));
						} else {
							status.setText("삭제할 데이터가 없습니다.");
							code.setText("");
							ea.setText("");
							serial.requestFocus();
						}
						rs.close();
						pstmt.close();
					} catch (Exception ex) {
						ex.printStackTrace();
					}

				}
			});
			button_1.setBounds(292, 10, 97, 23);
		}
		return button_1;
	}

	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("");
			status.setBounds(12, 200, 410, 15);
		}
		return status;
	}
}